<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends MX_Controller {


	function __construct()
	{
		parent::__construct();
		$this->load->model('listing/listing_model');
		$this->load->model('location/location_model');
		$this->load->model('category/category_model');
		$this->template->set_partial('nav_bar','header/nav_bar');
		$this->template->set_partial('header_search','header/header_search');
		$this->load->library('parser');
	}

	public function index(){


		$profile_id = $this->commoncall->get_id(current_url());

		$this->listing_model->update_visit($profile_id);

		$data['site_title'] = $this->settings_model->siteInfo('web_title');

		$data['profile']['details'] = $this->listing_model->getListing_deatils($profile_id);



		$data['term']['all'] = $this->listing_model->getkeyword($data['profile']['details']->id,$data['profile']['details']->parent,$data['profile']['details']->child);

		$data['qualification'] = $this->listing_model->getQualification($profile_id);



		$data['awards'] = $this->listing_model->getAwards($profile_id);



		$data['like']['counter'] = $this->listing_model->getLikes($profile_id);

		
		
		$data['like']['data'] = $this->listing_model->like_count_memwise(51,$profile_id);

		$data['unlike']['data'] = $this->listing_model->like_count_memwise(1,$profile_id);
		
		
		//$data['unlike']['data'] = $this->listing_model->countLikes();

		$data['association'] = $this->listing_model->getAssociations($profile_id);

		$data['license'] = $this->listing_model->getLicense($profile_id);

		$data['reviews'] = $this->listing_model->getReviews($profile_id);


		//--GET OVERALL REVIEW --//

		//** MAP **//
		$this->load->library('googlemaps');
		$config['zoom'] = 'auto';
		$config['map_type'] = 'ROADMAP';
		$config['trafficOverlay'] = 'TRUE';


		$this->googlemaps->initialize($config);


		$listing = $data['profile']['details'];


		$long_lat = $data['profile']['details']->lat.','.$data['profile']['details']->long;
		$marker = array();
		$marker['icon'] = base_url()."assets/images/map_icons/map_blue.png";
		$marker['position'] = $long_lat;
		$marker['infowindow_content'] = "<strong>$listing->title</strong>"."</br>"."$listing->address1"." $listing->address2";
		$marker['animation'] = 'DROP';
		$this->googlemaps->add_marker($marker);
		$data['map'] = $this->googlemaps->create_map();


		$data['module_banner'] = TRUE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = TRUE;
		$this->template->set_layout('inner_layout');

		$this->template->set_breadcrumb('Home', '/')
		//->set_breadcrumb('Category','')
		->set_breadcrumb('Category','category/all')
		->set_breadcrumb($data['profile']['details']->title,'');



		$listing_keyword = $this->listing_model->getKeywordAll($data['profile']['details']->id);

		$keyword = $data['profile']['details']->title;
		if($listing_keyword){
			foreach($listing_keyword  as $key){
				$keyword.= ", ".$this->listing_model->getCategory($key->term_value)->name." in ".$this->listing_model->getCity($data['profile']['details']->city);
			}
		}
		$this->template->title($data['profile']['details']->title, $data['site_title']);
		$this->template->set_metadata('keywords', $keyword);
		$this->template->set_metadata('description', $data['profile']['details']->description);

		$this->template->set_partial('listing_map', 'modules/listing_map');

		$this->template->build('profile',$data);
	}


	public function like($profile_id){

		$sql = "select * from profile where id=$profile_id " ;
		$result = $this->db->query($sql)->row();
		if(!$result){
			$result = NULL;
		}

		$data['profile']['details'] = $result;

		$profileurl = $data['profile']['details'];

		$memID = $this->session->userdata('mem_id');

		$data['like']['counter'] = $this->listing_model->getLikes($this->session->userdata['mem_id'])->liked_by;

		$this->form_validation->set_rules('profile_id', 'PROFILE ID');
		$this->form_validation->set_rules('like', 'Like');
		//$this->form_validation->set_rules('rev_ratting', 'Ratting', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('full_name','Full Name','trim|required|xss_clean');
		//$this->form_validation->set_rules('email_address','Email Address','trim|required|xss_clean|valid_email');
		//$this->form_validation->set_rules('rev_description', 'Description', 'trim|required|xss_clean|min_length[25]');

		if ($this->form_validation->run() == true) {
				
				
			$post_data=array(
					'liked_on' => $profile_id,
					'liked_by' => $this->session->userdata('mem_id'),
					'likes' => $this->input->post('like'),
					//'email_address' => $this->input->post('email_address'),
					//'experience' => $this->input->post('rev_ratting'),
					//'comment' => $this->input->post('rev_description'),
					//'posted_ip' => $this->commoncall->get_ip(),
			);

			//var_dump($post_data);
			//die();

			$this->db->insert('like_counter',$post_data);
			redirect('profile/'.$this->commoncall->url($profileurl->title).'-'.$profile_id);


		}

			
		/*	if($_POST['like']){
			$sql = "UPDATE like_counter set `like` = `like`+1 , `liked_by` = $memID where `id` = $profile_id ";
		$result=mysql_query($sql);
			
		if($result){
		redirect('profile/'.$this->commoncall->url($profileurl->title).'-'.$profile_id);
		}
		}*/
	}

	public function unlike($profile_id){

		$sql = "select * from profile where id=$profile_id " ;
		$result = $this->db->query($sql)->row();
		if(!$result){
			$result = NULL;
		}

		$data['profile']['details'] = $result;

		$profileurl = $data['profile']['details'];

		$memID = $this->session->userdata('mem_id');



		$this->form_validation->set_rules('profile_id', 'PROFILE ID');
		$this->form_validation->set_rules('unlike', 'Unlike');
		//$this->form_validation->set_rules('rev_ratting', 'Ratting', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('full_name','Full Name','trim|required|xss_clean');
		//$this->form_validation->set_rules('email_address','Email Address','trim|required|xss_clean|valid_email');
		//$this->form_validation->set_rules('rev_description', 'Description', 'trim|required|xss_clean|min_length[25]');

		if ($this->form_validation->run() == false) {
			$message = "Invalied input!";
		}
		else{
				
				
			$mem = $this->session->userdata('mem_id');
			$unlike = $this->input->post('unlike');
				
			$id = $this->input->post('id');
				
			$post_data=array(
					'liked_on' => $this->input->post('liked_on'),
					'liked_by' => $this->input->post('mem_id'),
					'likes' => $unlike,
						
			);

				
			$this->db->where('id',$id);
			$this->db->update('like_counter',$post_data);
			redirect('profile/'.$this->commoncall->url($profileurl->title).'-'.$profile_id);


		}
	}

/*
	public function unlike($id){

		$sql = "select * from profile where id=$id " ;
		$result = $this->db->query($sql)->row();
		if(!$result){
			$result = NULL;
		}

		$data['profile']['details'] = $result;

		$profileurl = $data['profile']['details'];

		$memID = $this->session->userdata('mem_id');

		$this->listing_model->likedata($id);
		redirect('profile/'.$this->commoncall->url($profileurl->title).'-'.$id);


	}

*/
	public function view_profile($profileID=0){

		//--Get PROFILE --//
		$data['profile']['info'] = $this->listing_model->getProfile($profileID);

		//--Get PROFILE TERM DATA --//
		$data['term']['data'] = $this->listing_model->getTerm($profileID = $profileID, $term = 'ALL', $exlude = 'KEY');

		$this->load->view('profile',$data);
	}

	public function listing_key($profileID=0){
		$tag = $this->listing_model->getTag($profileID);

		$tg='';
		if($tag){
			foreach ($tag as $tag){
				$catName = $this->category_model->getCategory_info($tag->term_value)->name;
				$tg .= "<li><a href=".base_url()."browse/category/".$this->commoncall->url($catName)."-".$tag->term_value ." > ".$catName."</a></li> ";
			}
			print $tg;
		}else{
			print '';
		}
	}

	function post_review()
	{
		$this->form_validation->set_rules('rev_profile_id', 'PROFILE ID', 'required');
		//$this->form_validation->set_rules('mem_id', 'Member');
		$this->form_validation->set_rules('rev_ratting', 'Ratting', 'trim|required|xss_clean');
		$this->form_validation->set_rules('rev_full_name','Full Name','trim|required|xss_clean');
		$this->form_validation->set_rules('rev_email_address','Email Address','trim|required|xss_clean|valid_email');
		$this->form_validation->set_rules('rev_description', 'Description', 'trim|required|xss_clean|min_length[25]');

		if ($this->form_validation->run() == false) {
			$message = "Invalied input!";
		}
		else{

			$post_data=array(
					'profile_id' => $this->input->post('rev_profile_id'),
					//'mem_id' => $this->session->userdata('mem_id'),
					'full_name' => $this->input->post('rev_full_name'),
					'email_address' => $this->input->post('rev_email_address'),
					'experience' => $this->input->post('rev_ratting'),
					'comment' => $this->input->post('rev_description'),
					//'posted_ip' => $this->commoncall->get_ip(),
			);

			$this->db->insert('review_master',$post_data);
			$message = "Review Submitted, waiting for moderation...";
		}

		$output = '{ "message": "'.$message.'"}';
		echo $output;
	}

	function post_enq()
	{


		$this->form_validation->set_rules('enqname', 'Your Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('enqemail', 'Your Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('enqdescription', 'Enter your mail', 'trim|required|xss_clean|min_length[50]');

		if ($this->form_validation->run() == false) {
			$message = "Invalied input!";
		}
		else{


			$email_template = $this->email_template_model->get_template_by_id(10);

			$lawyer_mail = $this->input->post('emailID');
				
			$website_title = $this->settings_model->get_web_configuration('web_title');

			$email_subject = $email_template->title;
			$email_body = $email_template->description;



			$data = array(
					//'prefix'	=> 'ENQ',
					//'listing_id' => $this->input->post('listing_id'),
					'wesite_title' => $website_title,
					'name' => $this->input->post('enqname'),
					'email' => $this->input->post('enqemail'),
					'message' => $this->input->post('enqdescription'),

					//'ip' => $this->commoncall->get_ip(),

			);


			$message = $this->parser->parse_string($email_body, $data, TRUE);
			$this->commoncall->send_mail($lawyer_mail, $email_subject, $message);

			$message = "Email Submitted....";
		}
			
		$output = '{ "message": "'.$message.'"}';
		echo $output;

	}
	function post($id)
	{


		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');

		$this->form_validation->set_rules('mail_description', 'Description', 'trim|required|xss_clean|min_length[25]');

		if ($this->form_validation->run() == false) {
			$message = "Invalied input!";
		}
		else{

			$profile_id = $id;


			$email_template = $this->email_template_model->get_template_by_id(10);

			$tutorial_master = $this->listing_model->getListing_deatils($profile_id);
				
				
			print_r($tutorial_master->email);
			die();
				
			//$lawyer_mail = $this->listing_model->getEmail($profile_id);
				
				

			$website_title = $this->settings_model->get_setting('webtitle');

			$email_subject = $email_template->title;

			$email_body = $email_template->description;

			$data = array(
					//'prefix'	=> 'ENQ',
					//'listing_id' => $this->input->post('listing_id'),
					//'website_title' => $website_title,
					'name' => $this->input->post('name'),
					'email' => $this->input->post('email'),
					'message' => $this->input->post('mail_description'),

					//'ip' => $this->commoncall->get_ip(),

			);


			$message = $this->parser->parse_string($email_body, $data, TRUE);
			$this->commoncall->send_mail($lawyer_mail, $email_subject, $message);

			$message = "Email Submitted, waiting for Reply...";
		}

		$output = '{ "message": "'.$message.'"}';
		echo $output;

	}
	/*
	 	
	$post_data=array(
			//'profile_id' => $this->input->post('profile_id'),
			//'mem_id' => $this->session->userdata('mem_id'),
			'name' => $this->input->post('name'),
			'email' => $this->input->post('email'),
			'description' => $this->input->post('mail_description'),
			//'posted_ip' => $this->commoncall->get_ip(),
	);

		
		
	$this->db->insert('message',$post_data);
		
	$email_template = $this->email_template_model->get_template_by_id(10);
		
	//var_dump($email_template);
	//die();
	$contact_master = $this->listing_model->getEmail($this->input->post('email'));
		
	$contact_name = $contact_master->name;
	$contact_email = $contact_master->email;
	$contact_message = $contact_master->message;
		
		
	//$website_title = $this->settings_model->get_setting('webtitle');
	//$website = $this->settings_model->get_setting('webfriendlyname');
	$default_mail = $this->listing_model->getListing_deatils($profile_id)->email;
		
	//$default_mail = $this->listing_model->get_mail($profile_email->email);
	//var_dump($default_mail);
	//die();
		
	//$cutomer_care = $this->settings_model->get_setting('callnow');
	$email_subject = $email_template->title;
	$email_body = $email_template->description;
		
	//	var_dump($email_body);
	//die();
		
		
	$data = array(
			'name' => $contact_name,
			'email' => $contact_email,
			'description' =>$contact_message,
			//'website' => $website,
			//'website_title' => $website_title,
			//'customer_care_no' => $cutomer_care
	);
		
	//var_dump($data);
	//die();
	$this->load->library('parser');
	$message = $this->parser->parse_string($email_body, $data, TRUE);
	$this->commoncall->send_mail($default_mail, $email_subject, $message);
		
		
	$message = "Email Submitted, waiting for Reply...";
	}

	$output = '{ "message": "'.$message.'"}';
	echo $output;
	}
	*/


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
